package com.lk.oauth2.config;

import java.io.IOException;

import org.apache.commons.io.IOUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;

/**
 * @description: Token配置类
 * @author: Li Kang
 * @create: 2020-08-30 16:23
 */
@Configuration
public class TokenConfig {

  @Bean
  public JwtAccessTokenConverter jwtAccessTokenConverter() {
    JwtAccessTokenConverter converter = new JwtAccessTokenConverter();

    ClassPathResource resource = new ClassPathResource("public.txt");
    String publicKey = null;
    try {
      publicKey = IOUtils.toString(resource.getInputStream(), "UTF-8");
    } catch (IOException e) {
      e.printStackTrace();
    }
    converter.setVerifierKey(publicKey);
    return converter;
  }

  @Bean
  public TokenStore tokenStore() {
    // 通过JWT管理token:这token包含了用户的信息
    return new JwtTokenStore(jwtAccessTokenConverter());
  }
}
